﻿using MES.Server.Common;
using MES.Server.Entities;
using MES.Server.IService;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace MES.Server.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class RoleController : ControllerBase
    {
        IRoleService _roleService;
        public RoleController(IRoleService roleService)
        {
            _roleService = roleService;
        }
        [HttpPost("list")]
        [Authorize]
        public async Task<JsonResult> GetRoleByIds(int[] ids)
        {
            ApiDataResult<RoleEntity[]> result = new ApiDataResult<RoleEntity[]>();
            try
            {
                result.Data = _roleService.GetRoleByIds(ids);
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }

            return await Task.FromResult(new JsonResult(result));
        }

        // api/role/all/none
        [HttpGet("all/{key}")]
        [Authorize]
        public async Task<JsonResult> GetAllRoles(string key)
        {
            ApiDataResult<RoleEntity[]> result = new ApiDataResult<RoleEntity[]>();
            try
            {
                key = key == "none" ? "" : key;
                result.Data = _roleService.GetAllRoles(key);
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }

            return await Task.FromResult(new JsonResult(result));
        }

        [HttpGet("check/{id}/{roleName}")]
        public async Task<JsonResult> CheckRoleName(string roleName, int id)
        {
            ApiDataResult<bool> result = new ApiDataResult<bool>();
            try
            {
                result.Data = _roleService.CheckRoleName(roleName, id);
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }

            return await Task.FromResult(new JsonResult(result));
        }

        [HttpPost("update")]
        [Authorize]
        public async Task<JsonResult> UpdateRole(RoleEntity entity)
        {
            ApiDataResult<int> result = new ApiDataResult<int>();
            try
            {
                var count = _roleService.Update(entity);
                result.Data = count;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }

        [HttpPost("delete")]
        [Authorize]
        public async Task<JsonResult> DeleteRole(int[] ids)
        {
            ApiDataResult<int> result = new ApiDataResult<int>();
            try
            {
                var count = _roleService.Delete(ids);
                result.Data = count;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }


        [HttpPost("update_menus")]
        [Authorize]
        public async Task<JsonResult> UpdateRoleMenus(RoleMenuEntity[] rms)
        {
            ApiDataResult<int> result = new ApiDataResult<int>();
            try
            {
                var count = _roleService.UpdateRoleMenus(rms);
                result.Data = count;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }

        [HttpPost("update_users")]
        [Authorize]
        public async Task<JsonResult> UpdateRoleUsers(RoleUserEntity[] rms)
        {
            ApiDataResult<int> result = new ApiDataResult<int>();
            try
            {
                var count = _roleService.UpdateRoleUsers(rms);
                result.Data = count;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }

        [HttpGet("del_user/{rid}/{uid}")]
        [Authorize]
        public async Task<JsonResult> DeleteRoleUser(int rid, int uid)
        {
            ApiDataResult<int> result = new ApiDataResult<int>();
            try
            {
                var count = _roleService.DeleteRoleUser(rid, uid);
                result.Data = count;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }
    }
}
